#include <iostream>
#include <queue>
using namespace std;
int N, M;
constexpr int MAXN = 100;
char Field[MAXN][MAXN];
int sx, sy, ex, ey;
void solve();
void bfs();
int main() {
    #ifndef ONLINE_JUDGE
    freopen("./input.txt", "r", stdin);
    // freopen("./out.txt", "w", stdout);
    #endif
    cin >> N >> M;
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++) {
            cin >> Field[i][j];
            if (Field[i][j] == 'S') {
                sx = i; sy = j;
            }
            if (Field[i][j] == 'G') {
                ex = i; ey = j;
            }
        }
    bfs();
}

struct Step {
    pair<int, int> coordinate;
    int step;
    Step(int _x, int _y, int _step): 
        coordinate(_x, _y), step(_step) { }
    Step(const pair<int, int> & _coordinate, int _step):
        coordinate(_coordinate), step(_step) { } 

};

int dxdy[][4] = {
    {-1, 0, 1, 0},
    {0, -1, 0, 1}
};

bool check(int x, int y) {
    if (x >= 0 && x < N && y >= 0 && y < M && Field[x][y] != '#') return true;
    return false;
}

void bfs() {
    queue<Step> q;
    q.push(Step(sx, sy, 0));
    while (!q.empty()) {
        Step temp = q.front(); q.pop();
        if (temp.coordinate.first == ex && temp.coordinate.second == ey) {
            cout << temp.step << endl;
            break;
        }
        Field[temp.coordinate.first][temp.coordinate.second] = '#';
        int step = temp.step;
        for (int i = 0; i < 4; i++) {
            int nx = temp.coordinate.first + dxdy[0][i];
            int ny = temp.coordinate.second + dxdy[1][i];
            if (check(nx, ny)) {
                q.emplace(nx, ny, step + 1);
            }
        }
    }
}